var Tasklog=Tasklog||{}
Tasklog.Toolbar=(function(Tasklog){ return {
	menuTimeout:null, activeMenu:null, menuAutoClose: false, menuEffect: 'slide',
	init:function(){
		var self=this;
		$toolbar=$('menu[type=toolbar]');
		$toolbar.click(function(e){e.stopPropagation()})
		$toolbar.find('li').click(function(e){ 
			var action=(this.title||$('span',this)[0].title).toLowerCase().replace(/ /g,'');
			self.doAction(action, this);
			e.stopPropagation();
		});
		$toolbar.find('.mn-trash').mouseleave(function(){ var el=$(this);if(!el.hasClass('notext')||el.queue().length) self.deleteConfirm(el,true) });
		
		if (this.menuAutoClose){
			$toolbar.find('.hasmenu').bind('mouseleave', function(){self.menuTimer()});
			$toolbar.find('.menu').bind('mouseleave', function(){self.menuTimer()});
			$toolbar.find('.menu').bind('mouseenter', function(){self.menuShow($(this).prev('.button')[0])});
		}
		
		$('#tagmenu').css('left',$('#tagmenu').prev('.button').offset().left);		
		Tasklog.Tags.drawMenu(); //tags
	}
	,doAction: function(action, target){
		this.menuHide();
		var self=this;
		switch(action){
			case 'tag'				: this.menuShow(target,true);			break;
			case 'add'				: this.actionAdd();						break;	
			case 'delete'			: this.actionDelete(target);			break;	
			case 'showcompleted'	: this.actionShowCompleted(target);	break;	
			case 'hidecompleted'	: this.actionHideCompleted(target);	break;	
			case 'print'			: this.actionPrint();					break;	
			case 'refresh'			: this.actionRefresh();					break;	
			case 'help'				: this.actionHelp();						break;
			
			case 'tagmanager'		: Tasklog.Tags.tagManager();			break;
			default: msgbox(action);
		}
	}
	
	
	,menuShow: function(target,isClick){
		this.menuCancelTimer();
		if(this.activeMenu){
			var btn=this.activeMenu.prev('.button')[0];
			if (target==btn){ if (isClick)this.menuHide(); return; }
			else this.menuHide();
		} 
		this.activeMenu=$(target).next('.menu');
		if (this.menuEffect=='fade') this.activeMenu.fadeIn('fast');
		else this.activeMenu.show('fast');
	}
	,menuHide: function(){
		if (!this.activeMenu) return;
		if(this.activeMenu.is(':visible')){
			if (this.menuEffect=='fade') this.activeMenu.fadeOut('fast');
			else this.activeMenu.hide('fast');
			this.activeMenu=null; 
		} 
	}
	,menuTimer: function(){ this.menuCancelTimer(); var self=this; this.menuTimeout=setTimeout(function(){self.menuHide()},1000); }
	,menuCancelTimer: function(){ clearTimeout(this.menuTimeout); this.menuTimeout=null; }
	
	
	,actionAdd: function(){ Tasklog.Task.rowAdd($('.task-table:first-of-type tbody'), 'prepend') }

	,deleteConfirm: function(el, off){
		var self=this; el.stop();
		if (off){ 
			if (!el.hasClass('notext')) el.addClass('notext').css({width:'53px'}); 
			el.animate({ width: "16px" }, { duration:300 })
		} 
		else el.animate({ width: "53px" }, { duration:200, complete: function(){ $(this).removeClass('notext').css({width:'auto'}); } }) 
	}
	,actionDelete: function(target){
		var row=$('table.task-table tr.task-edit'), btn=$(target);
		if (btn.hasClass('notext')) this.deleteConfirm(btn);
		else { 
			btn.css('width','16px').addClass('notext'); 
			if (!row.length) msgerror('Select a row'); 
			else Tasklog.Task.rowRemove(row,true);	
		}
	}
	
	,actionShowCompleted: function(target){
		var btn=$('span',target);
		btn.attr('title','Hide Completed').html('Hide Completed');
		Tasklog.drawTasks(true)
	}
	,actionHideCompleted: function(target){
		var btn=$('span',target);
		btn.attr('title','Show Completed').text('Show Completed');
		Tasklog.drawTasks()
	}

	,actionPrint: function(){ window.print() }

	,actionRefresh: function(){ 
		//location.reload()
		Tasklog.drawTasks();
	}

	,actionHelp: function(){
		var help='<table class="helpText">'
			+'<tr><td>up/down</td><td>navigate up/down the tasks content</td></tr>'
			+'<tr><td>tab/shift+tab</td><td>move to the next/previous task</td></tr>'
			+'<tr><td>ctrl+up/down</td><td>move the current taks up/down</td></tr>'
			+'<tr><td>ctrl+space</td><td>check/uncheck the taks</td></tr>'
			+'<tr><td>?</td><td>help window</td></tr>'
			+'</table>';
		Tasklog.Window.show(help,'Help', true, true);	
	}
}})(Tasklog);